package data

import (
	"go.manyji.com/lib/app"
	"go.manyji.com/lib/config"
	"go.manyji.com/lib/mysql"
)

var (
	fDBAddr = config.String("db.mysql.addr", "127.0.0.1:3306", "mysql addr")
	fDBName = config.String("db.mysql.name", "reader", "mysql db name")
	fDBUser = config.String("db.mysql.user", "root", "mysql db user")
	fDBPwd  = config.String("db.mysql.pwd", "", "mysql db password")

	db  *mysql.DB
	fns []func()
)

func init() {
	app.OnStart(func() {
		db, _ = mysql.Open(*fDBAddr, *fDBUser, *fDBPwd, *fDBName)
		for _, fn := range fns {
			app.AsyncSafe(fn)
		}
	})
}

func DB() *mysql.DB {
	return db
}

func AfterDBConnect(fn func()) {
	fns = append(fns, fn)
}
